//
//  ViewController.swift
//  ScrollViewDemo
//
//  Created by wujg on 2018/5/22.
//  Copyright © 2018年 wujg. All rights reserved.

let kScreenW = UIScreen.main.bounds.size.width
let kScreenH = UIScreen.main.bounds.size.height
let SumNumber = 10
import UIKit
class ViewController: UIViewController {
    lazy var View1: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: kScreenH * 0, width: kScreenW, height: kScreenH))
        view.backgroundColor = UIColor.red
        return view
    }()
    lazy var View2: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: kScreenH * 1, width: kScreenW, height: kScreenH))
        view.backgroundColor = UIColor.yellow
        return view
    }()
    lazy var View3: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: kScreenH * 2, width: kScreenW, height: kScreenH))
        view.backgroundColor = UIColor.blue
        return view
    }()
    lazy var View4: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: kScreenH * 3, width: kScreenW, height: kScreenH))
        view.backgroundColor = UIColor.lightGray
        return view
    }()
    lazy var View5: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: kScreenH * 4, width: kScreenW, height: kScreenH))
        view.backgroundColor = UIColor.white
        return view
    }()
    lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView(frame: CGRect.init(x: 0, y: 0, width: kScreenW, height: kScreenH))
        scroll.contentSize = CGSize(width: kScreenW, height: kScreenH * 5)
        scroll.delegate = self
        scroll.isPagingEnabled = true
        return scroll
    }()
    var colorArr = Array<UIColor>()
    var index = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(scrollView)
        for _ in 0...SumNumber {
            let color = UIColor.randomColor
            colorArr.append(color)
        }
        self.scrollView.addSubview(View1)
        self.scrollView.addSubview(View2)
        self.scrollView.addSubview(View3)
        self.scrollView.addSubview(View4)
        self.scrollView.addSubview(View5)
    }
    func refreshView() {
        self.scrollView.contentOffset = CGPoint.init(x: 0, y: kScreenH * 2)
        View1.backgroundColor = self.colorArr[index - 2]
        View2.backgroundColor = self.colorArr[index - 1]
        View3.backgroundColor = self.colorArr[index - 0]
        View4.backgroundColor = self.colorArr[index + 1]
        View5.backgroundColor = self.colorArr[index + 2]
    }
    func moreData() {
        index = index + 1
        if index + 2 < SumNumber {
            refreshView()
        }
    }
    func lessData() {
        index = index - 1
        if index - 2 > 0 {
            refreshView()
        }
    }
}
extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y > kScreenH * 2.5 && scrollView.contentOffset.y < kScreenH * 3.5 && index <= SumNumber {
            if Int(kScreenH) * index < Int(scrollView.contentOffset.y) {//上滑
                print("加一   \(index)   \(Int(kScreenH) * index)    \(Int(scrollView.contentOffset.y))")
                index = index + 1
            }
        }
        if scrollView.contentOffset.y > kScreenH * 0.5 && scrollView.contentOffset.y < kScreenH * 1.5 && index > 2 {
            if Int(kScreenH) * index > Int(scrollView.contentOffset.y) {//下滑
                print("减一   \(index)   \(Int(kScreenH) * index)    \(Int(scrollView.contentOffset.y))")
                index = index - 1
            }
        }
        if kScreenH * 3 < scrollView.contentOffset.y && index <= SumNumber {
            print("加一   \(index)   \(Int(kScreenH) * index)    \(Int(scrollView.contentOffset.y))")
            moreData()
        }else if kScreenH * 1 > scrollView.contentOffset.y && index > 2 {
            print("减一   \(index)   \(Int(kScreenH) * index)    \(Int(scrollView.contentOffset.y))")
            lessData()
        }
    }
}
extension UIColor {
    //返回随机颜色
    open class var randomColor:UIColor{
        get
        {
            let red = CGFloat(arc4random()%256)/255.0
            let green = CGFloat(arc4random()%256)/255.0
            let blue = CGFloat(arc4random()%256)/255.0
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
}
